var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const pay = require('../../../services/pay.js');
const app = getApp()
// 触底上拉刷新 TODO 这里要将page传给服务器，作者没写
Page({
    data: {
        orderList: [],
        showuserid:0,
        allOrderList1: [],
        allPage: 1,
        allCount: 0,
        size: 8,
        showType: 9,
        hasOrder: 0,
        showTips: 0,
        status: {},
        nowShowType:'',
        userId:'',
        expressStatus:''
    },
    toOrderDetails: function(e) {
        // let that=this
        let orderId = e.currentTarget.dataset.id;
        
        wx.setStorageSync('orderId', orderId)
        wx.navigateTo({
            url: '/pages/ucenter/order-details/index',
        })
    },
    payOrder: function(e) {
        let orderId = e.currentTarget.dataset.orderid;
        let that = this;
        // console.log("我在这儿")
        // console.log(orderId)
        pay.payOrder(parseInt(orderId)).then(res => {
            let showType = wx.getStorageSync('showType');
            console.log(showType)
            that.setData({
                showType: showType,
                orderList: [],
                allOrderList: [],
                allPage: 1,
                allCount: 0,
                size: 8
            });
            that.getOrderList();
            that.getOrderInfo();
        }).catch(res => {
            util.showErrorToast(res.errmsg);
        });
    },
    //点击确认收获变成已发货状态
    fahuo: function(e) {
      let orderId = e.currentTarget.dataset.orderid;
      let that = this;
      console.log("我在这儿")
      console.log(orderId)
      util.request(api.PostExpress+orderId).then(function(res){
        wx.showToast({
          title: '收货成功'
      });
      })
    },
    //跳转评价页面
    tzpjym: function(e) {
      let orderId = e.currentTarget.dataset.orderid;
      let that = this;
      console.log("我在这儿")
      console.log(orderId)
      wx.navigateTo({
        url: '/pages/ucenter/comment/comment?orderid='+orderId,
    })
      
    },
    getOrderInfo: function(e) {
        let that = this;
        util.request(api.OrderCountInfo).then(function(res) {
            if (res.errno === 0) {
                let status = res.data;
                that.setData({
                    status: status
                });
            }
        });
    },
    getOrderList() {
        let that = this;
      //   util.request(api.OrderList, {
      //     showType: that.data.showType,
      //     size: that.data.size,
      //     page: that.data.allPage,
      // })
      console.log("-------------------------------")
        util.request(api.OrderUserList,{
            userId:that.data.showuserid,
            expressStatus: that.data.showType}
            
          ).then(function(res) {
          // console.log(that.data.showType)
          
            if (res.code === 20000) {
              
              that.setData({
                // allCount: count,
                // allOrderList: that.data.allOrderList.concat(res.data.data),
                allOrderList1:res.data,
                // allPage: res.data.currentPage,
                // orderList: that.data.allOrderList.concat(res.data.data)
            });

            console.log(that.data.allOrderList1)
            
            }
        });
    },
    toIndexPage: function(e) {
        wx.switchTab({
            url: '/pages/index/index'
        });
    },
    onLoad: function() {},
    onShow: function() {
        let showType = wx.getStorageSync('showType');
        // 将url传过来的showType赋值给nowShowType
        let nowShowType = this.data.showType;
        let showuserid=wx.getStorageSync('showUserId');
        let doRefresh = wx.getStorageSync('doRefresh');
        
        if (nowShowType != showType || doRefresh == 1) {
            this.setData({
                showType: showType,
                showuserid:showuserid,
                orderList: [],
                allOrderList: [],
                allPage: 1,
                allCount: 0,
                size: 8,
                nowShowType: showType
            });
            this.getOrderList();
            wx.removeStorageSync('doRefresh');
        }
        this.getOrderInfo();
    },
    switchTab: function(event) {
        let showType = event.currentTarget.dataset.index;
        console.log(showType)
        wx.setStorageSync('showType', showType);
        this.setData({
            showType: showType,
            orderList: [],
            allOrderList1: [],
            allPage: 1,
            allCount: 0,
            size: 8
        });
        this.getOrderInfo();
        this.getOrderList();
    },
    // “取消订单”点击效果
    cancelOrder: function(e) {
        let that = this;
        let orderId = e.currentTarget.dataset.index;
        wx.showModal({
            title: '',
            content: '确定要取消此订单？',
            success: function(res) {
                if (res.confirm) {
                    util.request(api.OrderCancel, {
                        orderId: orderId
                    }, 'POST').then(function(res) {
                        if (res.errno === 0) {
                            wx.showToast({
                                title: '取消订单成功'
                            });
                            that.setData({
                                orderList: [],
                                allOrderList: [],
                                allPage: 1,
                                allCount: 0,
                                size: 8
                            });
                            that.getOrderList();
                        } else {
                            util.showErrorToast(res.errmsg);
                        }
                    });
                }
            }
        });
    },
    onReachBottom: function() {
        let that = this;
        if (that.data.allCount / that.data.size < that.data.allPage) {
            that.setData({
                showTips: 1
            });
            return false;
        }
        that.setData({
            'allPage': that.data.allPage + 1
        });
        that.getOrderList();
    }
})